package h.c.z.designPattern.C02_SingletonPattern;

import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * 读取配置文件 no design
 * @author jinsicao
 */
public class AppConfig {

	/**
	 * 用来存放参数
	 */
	private ResourceBundle resourceBundle;
	
	public AppConfig() {
		// 调用读取配置文件的方法
		readConfig();
	}
	
	public String getValueByKey(String key) {
		if (key != null && key.length() > 0) {
			return this.resourceBundle.getString(key);
		}
		return null;
	}

	private void readConfig() {
		InputStream in = null;
		
		try {
			// 读取配置文件信息
			in = AppConfig.class.getResourceAsStream("app.properties");
			
			// 设置配置文件参数信息
			this.resourceBundle = new PropertyResourceBundle(in);
			
		} catch (Exception e) {
			System.out.println("装载配置文件出错了：");
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		AppConfig appConfig = new AppConfig();
		System.out.println(appConfig.getValueByKey("systemKey"));
		System.out.println(appConfig.getValueByKey("queueNum"));
	}
}
